/**
* coder  : dzlua
* email  : 505544956@qq.com
* module : ApiMgr-client
* path   : src/router
* file   : index.js
* time   : 2017-11-14 17:00:43
*/

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

let router = new Router({
  routes: [
    {
      path: '/',
      name: 'home',
      meta: {
        login_required: true
      },
      component: resolve => require(['../views/home.vue'], resolve)
    }, {
      path: '/register',
      name: 'register',
      component: resolve => require(['../views/register.vue'], resolve)
    }, {
      path: '/login',
      name: 'login',
      component: resolve => require(['../views/login.vue'], resolve)
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.meta.login_required) {
    let user = JSON.parse(window.localStorage.getItem('user'))
    if (!user) {
      next({ name: 'login' })
      return
    }
  }
  next()
})

export default router
